package mvp.com.bbc.newrelyframes.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ${cbf} on 2017/12/5.
 */

public abstract class BaseFragment extends Fragment {

    private View baseView;
    private Unbinder unbinder = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        baseView = inflater.inflate(baseFragmentLayout(), container, false);
        ButterKnife.bind(this, baseView);
        return baseView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        initView();
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null)
            unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    //父类提供一个方法个子类实现
    protected abstract int baseFragmentLayout();

    //父类提供一个方法个子类实现
    protected abstract void initView();

    //父类提供一个方法个子类实现
    protected abstract void initData();

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(String action) {

    }
}
